package com.apes.scm.voucher.service.cost;

import com.apes.scm.account.account.model.CostingType;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.DeptEnum;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherInventory;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 功能：采购订单成本核算
 *
 * @author xul
 * @create 2018-10-18 9:26
 */
@Component
public class PurchaseOrderCostMethod extends AbstractCostMethod {

    public PurchaseOrderCostMethod() {
        this.BusinessTypes = new String[]{BusinessType.PURCHASE_ORDER};
    }

    /**
     * 成本核算/价值评估
     */
    @Override
    public void costing(Voucher voucher) {
        Set<VoucherInventory> voucherInventories = new HashSet();
        voucher.getVoucherItems().stream().forEach((voucherItem) -> {
            InventoryBatch inventoryBatch = createInventoryBatch(voucher, voucherItem);
            VoucherInventory voucherInventory = createVoucherInventory(voucherItem, inventoryBatch, voucherItem.getProductQty());
            voucherInventories.add(voucherInventory);
        });
        valuationService.save(voucherInventories);
    }

    /*
    * 创建批次
    * */
    private InventoryBatch createInventoryBatch(Voucher voucher, VoucherItem voucherItem) {
        InventoryBatch inventoryBatch = new InventoryBatch();
        inventoryBatch.setCompany(voucher.getCompany());
        inventoryBatch.setContract(voucherItem.getVoucher().getContract());
        Dept dept = voucherItem.getVoucher().getDept();
        if (!dept.existDeptType(DeptEnum.PURCHASE)) {
            throw new RuntimeException("业务凭证部门为非采购部门！");
        }
        inventoryBatch.setDept(dept);
        inventoryBatch.setSupplier(voucherItem.getVoucher().getPartner());
        inventoryBatch.setCooperation(voucher.checkCooperation());
        inventoryBatch.setPurchaseInDate(voucherItem.getVoucher().getCreateDate());
        inventoryBatch.setBatchNumber(voucherItem.getId());
        inventoryBatch.setBatchSerial(0);
        inventoryBatch.setProduct(voucherItem.getProduct());
        inventoryBatch.setTax(voucherItem.getTax());
        inventoryBatch.setUom(voucherItem.baseUom());
        inventoryBatch.setNoTaxCostPrice(voucherItem.computePriceUnit(voucherItem.computeExcludedPriceUnit()));
        inventoryBatch.setTaxCostPrice(voucherItem.computeBasePrice());
        double qty = voucherItem.computeBaseQty();
        inventoryBatch.setPurchaseQty(qty);
        inventoryBatch.changeBalanceQty(qty);
        return save(inventoryBatch);
    }

    /**
     * 功能：商品采购库存价值评估（OperateType.IN_SUPPLIER）
     */
    public InventoryBatch save(InventoryBatch inventoryBatch) {
        String costingMethod = inventoryService.getCostingMethod(inventoryBatch.getCompany(), inventoryBatch.getProduct());
        if (Objects.equals(costingMethod, CostingType.CM_FIFO)) {
            return inventoryService.save(inventoryBatch);
        } else {
            throw new RuntimeException("存货计价方法" + costingMethod + " 暂未实现！");
        }
    }

}
